import Common from "../Common/common";
import { Data } from "../Data/data";
import Box = Laya.Box;
import Image = Laya.Image;
import Text = Laya.Text;
import Handler = Laya.Handler;

export default class WorldRankPageCtrl extends Laya.View {
    [x: string]: any;

    private rankList;
    public fractionNum

    constructor() {
        super();
    }
    onAwake() {
        //头条直接初始化世界排行榜
        if (Common.platform == "toutiao") {
            this.bg.skin = "images/rankingList/bg7.png"
            this.title.visible = true
            this.initWorldRank();
        } else {

            // this.initFriendRank();//初始化好友排行榜

        }
        this.userInfo()
    }

    onOpened(args) {
        let type = args.type
        if (type == "home") {
            this.fraction.visible = false
            if (Common.getCacheItem("fraction") != null) {
                this.fractionNum = Common.getCacheItem('fraction')
            } else {
                this.fractionNum = 0
            }
        } else if (type == "over") {
            this.fraction.visible = true
            var filter: Laya.GlowFilter = new Laya.GlowFilter("#936bed", 5, 0, 0);
            this.fraction.filters = [filter, filter]
            this.fraction.text = "本次得分" + args.fractionNum
            this.fractionNum = args.fractionNum
        }
        //侦听tab
        if (Common.platform == "weixin") {
            this.tab.selectHandler = new Laya.Handler(this, this.selectTab);
        }
        this.btnEvent()
    }

    /**
     * 监听鼠标事件
     */
    btnEvent() {
        this.start.on(Laya.Event.CLICK, this, () => {
            Laya.Scene.open("gameView.scene");
            Laya.stage.event("playSound", ["sound/tap.mp3"])
        });
        this.return.on(Laya.Event.CLICK, this, () => {
            this.close()
            Laya.stage.event("playSound", ["sound/tap.mp3"])
        })
        this.share.on(Laya.Event.CLICK, this, () => {
            Laya.stage.event("playSound", ["sound/tap.mp3"])
            Common.shareTitle = "我在我要穿越中得了" + this.fractionNum + "分，不服来战"
            Laya.stage.event("wxShare", [7, ''])
        })
    }

    // initFriendRank(){
    //     this.my.visible = false;
    //     this.rangList.visible = false;
    //     this.openData.visible = true;
    //     this.tab.visible = true;

    //     let obj:any = {};
    //     obj.type = "updateBaseFriendData";//先更新初始化好友数据
    //     this.openData.postMsg(obj);
    //     obj.type = "initRank";
    //     this.openData.postMsg(obj);//先开始初始化界面
    //     obj.type = "initFriendList";
    //     obj.openid = Dataa.UserData.openid;
    //     this.openData.postMsg(obj);//先获取我的记录，再更新
    // }

    selectTab(index) {
        console.log(index)
        if (index == 0) {
            // this.initFriendRank();
            Laya.stage.event("playSound", ["sound/tap.mp3"])
        } else {
            Laya.stage.event("playSound", ["sound/tap.mp3"])
            this.initWorldRank();
        }
        console.log(index);
    }

    // onClosed(){
    //     let obj:any = {};
    //     obj.type = "close";
    //     this.openData.postMsg(obj);
    // }

    // share(){
    //     Laya.stage.event("WXShare",["rank"]);
    // }


    initWorldRank() {
        // this.openData.visible = false;
        if (Common.platform == "weixin") {
            this.tab.visible = true;
        }
        // this.my.visible = true;
        this.rangList.visible = true;

        var yhr = new Laya.HttpRequest();
        yhr.http.timeout = 10000;
        yhr.once(Laya.Event.ERROR, this, this.yErrorHandler);
        yhr.once(Laya.Event.COMPLETE, this, this.yCompleteHandler);
        yhr.send(Common.requestBaseUrl + "/game/totalRankList?page=1&pageSize=100&tags=pass&token=" + Common.getCacheItem('loginToken'), "", "get", "json")
    }

    public updateMyInfo(arr) {
        this.myInfo.visible = true
        if(Common.getCacheItem("avatarUrl") != ''){
            this.myHead.skin = Common.getCacheItem("avatarUrl");
        }
        this.myName.text = Common.getCacheItem("nick");
        this.myScore.text = arr == null || arr.totalScore == null ? "0分" : arr.totalScore + "分";
        if (arr != null) {
            if (arr.rank <= 3) {
                this.myRankImg.skin = "images/rankingList/NO" + arr.rank + ".png"
                this.myRankImg.visible = true;
                this.myRank.visible = false;
            } else {
                if (arr.rank == null || arr.rank > 999) {
                    this.myRank.text = "999+";
                    this.myRank.fontSize = 36
                } else {
                    this.myRank.text = arr.rank.toString();
                }
                //this.myRank.text = arr.index;
                this.myRankImg.visible = false;
                this.myRank.visible = true;
            }
        } else {
            this.myRank.text = "999+";
            this.myRank.fontSize = 30
            this.myRankImg.visible = false;
            this.myRank.visible = true;
        }
    }

    userInfo() {
        if(Common.getCacheItem("avatarUrl") != ''){
            this.wxHeadImg.skin = Common.getCacheItem("avatarUrl");
        }
        this.wxName.text = Common.getCacheItem("nick");
    }




    /**请求错误时调用 */
    yErrorHandler(data) {
        Laya.stage.event("ShowMsg", "加载排行榜有问题，请重新打开!");
        console.log("请求错误:" + data);
    }

    /**请求结束时调用，获取服务器数据 */
    yCompleteHandler(data) {
        console.log("请求排行榜数据成功")
        console.log(data);
        if (data != null && data.code == 0) {
            this.rankList = data.data.baseData.list;
            let myRank = data.data.myRank;
            this.updateList();
            this.updateMyInfo(myRank);
        } else {
            Laya.stage.event("ShowMsg", "加载排行榜有问题，请重新打开!");
        }
    }



    updateList() {
        var list = this.rangList;
        list.itemRender = Item;

        // 使用但隐藏滚动条
        list.vScrollBarSkin = "";
        list.selectEnable = true;
        list.selectHandler = new Handler(this, this.onSelect);
        list.renderHandler = new Handler(this, this.updateItem);


        list.array = this.rankList;
    }

    private updateItem(cell: Item, index: number): void {
        //cell.setIcon(cell);
        //cell.setText1(cell.dataSource);
        // cell.setBtn(cell.dataSource);

        let n = cell.dataSource.index;

        cell.imageHead.skin = cell.dataSource.portrait;
        cell.imageHead = Common.transferImageToCircle(cell.imageHead);
        cell.textName.text = cell.dataSource.name;
        cell.textScore.text = Common.transferNoNullValue(cell.dataSource.totalScore, 0) + "分";
        let t = Math.floor(cell.dataSource.totalScore / 20);
        if (cell.dataSource.rank <= 3) {
            cell.imageRank.skin = "images/rankingList/NO" + cell.dataSource.rank + ".png"
            cell.imageRank.visible = true;
            cell.textRank.visible = false;
        } else {
            cell.textRank.text = cell.dataSource.rank;
            cell.imageRank.visible = false;
            cell.textRank.visible = true;
        }

    }
    private onSelect(index: number): void {
        console.log("当前选择的索引：" + index);
    }


}



class Item extends Box {
    public static WID: number = 862;
    public static HEI: number = 169;

    textRank: Laya.Label;//排行文字
    textName: Laya.Text;//昵称
    textScore: Laya.Text;//分数
    imageHead: Image;//头像图片
    line: Image;//直线
    imageRank: Image;//排行图片
    // textLevel:Laya.Label;//等级

    constructor() {
        super();
        this.init();
    }




    init() {
        this.size(Item.WID, Item.HEI);
        this.line = new Image();
        this.line.x = 0;
        this.line.y = 166;
        this.line.width = 862;
        this.line.height = 3;
        this.line.skin = "images/rankingList/_line.png"


        this.imageRank = new Image();
        this.imageRank.x = 51;
        this.imageRank.y = 45;
        this.imageRank.width = 84;
        this.imageRank.height = 84;
        // this.imageRank.skin="myResource/rank/no1.png"
        this.imageRank.visible = false;

        let b = new Laya.Box();
        b.x = 59
        b.y = 63.5
        b.width = 117
        b.height = 80
        this.textRank = new Laya.Label();
        this.textRank.fontSize = 60;
        this.textRank.color = "#ffffff";
        this.textRank.bold = true;
        this.textRank.x = 11;
        this.textRank.y = 8;
        // this.textRank.font = Common.textFont;
        // this.textRank.centerX = 1;
        // this.textRank.centerY = 1;
        b.addChild(this.textRank);

        this.imageHead = new Image();
        this.imageHead.x = 169;
        this.imageHead.y = 35;
        this.imageHead.width = 100;
        this.imageHead.height = 100;
        // this.imageHead.skin="myResource/rank/head.png"



        this.textName = new Text();
        this.textName.fontSize = 42;
        this.textName.color = "#ffffff";
        // this.textName.bold = true;
        this.textName.x = 320;
        this.textName.y = 68;
        // this.textName.width = 218;
        this.textName.height = 42;
        // this.textName.font = Common.textFont;

        this.textScore = new Text();
        this.textScore.fontSize = 42;
        this.textScore.color = "#ffffff";
        // this.textScore.bold = true;
        this.textScore.x = 676;
        this.textScore.y = 68;
        // this.textScore.font = Common.textFont;

        let sc = new Laya.Image();
        sc.x = 507
        sc.y = 22;
        // sc.skin = "myResource/rank/bg.png";

        // this.textLevel = new Laya.Label();
        // this.textLevel.fontSize = 26;
        // this.textLevel.color = "#fefefe";
        // this.textLevel.bold = true;
        // // this.textLevel.font = Common.textFont;
        // this.textLevel.centerX = 1;
        // this.textLevel.centerY = 1;
        // sc.addChild(this.textLevel);


        this.addChild(this.line);
        this.addChild(this.imageRank);
        this.addChild(b);
        this.addChild(this.imageHead);
        this.addChild(this.textName);
        this.addChild(this.textScore);
        this.addChild(sc);
    }




}